package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AwMyPlan;
import com.ruoyi.system.mapper.AwMyPlanMapper;
import com.ruoyi.system.service.IAwMyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 我的计划Service业务层处理
 *
 * @author anxinr
 * @date 2023-08-15
 */
@Service
public class AwMyPlanServiceImpl extends ServiceImpl<AwMyPlanMapper ,AwMyPlan> implements IAwMyPlanService
{
    @Autowired
    private AwMyPlanMapper awMyPlanMapper;

    /**
     * 查询我的计划
     *
     * @param id 我的计划主键
     * @return 我的计划
     */
    @Override
    public AwMyPlan selectAwMyPlanById(Long id)
    {
        return awMyPlanMapper.selectById(id);
    }

    /**
     * 查询我的计划列表
     *
     * @param awMyPlan 我的计划
     * @return 我的计划
     */
    @Override
    public List<AwMyPlan> selectAwMyPlanList(AwMyPlan awMyPlan)
    {
        LambdaQueryWrapper<AwMyPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!Objects.isNull(awMyPlan.getUserId()) ,AwMyPlan::getUserId ,awMyPlan.getUserId()) ;
        wrapper.like(StringUtils.isNotEmpty(awMyPlan.getName()) ,AwMyPlan::getName ,awMyPlan.getName()) ;
        wrapper.like(StringUtils.isNotEmpty(awMyPlan.getPlanDescribe()) ,AwMyPlan::getPlanDescribe ,awMyPlan.getPlanDescribe()) ;
        wrapper.eq(!Objects.isNull(awMyPlan.getStatus()) ,AwMyPlan::getStatus ,awMyPlan.getStatus()) ;
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            wrapper.eq(AwMyPlan::getUserId ,SecurityUtils.getUserId()) ;
        }
        return awMyPlanMapper.selectList(wrapper);
    }

    /**
     * 新增我的计划
     *
     * @param awMyPlan 我的计划
     * @return 结果
     */
    @Override
    public int insertAwMyPlan(AwMyPlan awMyPlan)
    {
        awMyPlan.setCreateTime(DateUtils.getNowDate());
        awMyPlan.setUserId(SecurityUtils.getUserId());
        return awMyPlanMapper.insert(awMyPlan);
    }

    /**
     * 修改我的计划
     *
     * @param awMyPlan 我的计划
     * @return 结果
     */
    @Override
    public int updateAwMyPlan(AwMyPlan awMyPlan)
    {
        awMyPlan.setUpdateTime(DateUtils.getNowDate());
        return awMyPlanMapper.updateById(awMyPlan);
    }

    /**
     * 批量删除我的计划
     *
     * @param ids 需要删除的我的计划主键
     * @return 结果
     */
    @Override
    public int deleteAwMyPlanByIds(Long[] ids)
    {
        if (!CollectionUtils.isEmpty(Arrays.asList(ids))){
            for (int i = 0; i < ids.length; i++) {
                this.removeById(ids[i]) ;
            }
            return ids.length;
        }
        return 0;
    }

    /**
     * 删除我的计划信息
     *
     * @param id 我的计划主键
     * @return 结果
     */
    @Override
    public int deleteAwMyPlanById(Long id)
    {
        return awMyPlanMapper.deleteAwMyPlanById(id);
    }
}
